package cn.cnf.service.impl;

import cn.cnf.common.exception.CustomException;
import cn.cnf.entity.NovelDetails;
import cn.cnf.mapper.NovelDetailsMapper;
import cn.cnf.service.NovelDetailsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 服务接口实现
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-04 15:18:43
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Service
@Transactional(rollbackFor = {RuntimeException.class, Error.class})
public class NovelDetailsServiceImpl extends ServiceImpl<NovelDetailsMapper, NovelDetails> implements NovelDetailsService {

    private final NovelDetailsMapper novelDetailsMapper;

    @Override
    public NovelDetails getNovelDetails(String novelId, String chapterId) {
        LambdaQueryWrapper<NovelDetails> wrapper = new LambdaQueryWrapper<NovelDetails>()
                .eq(NovelDetails::getNovelId, novelId)
                .eq(NovelDetails::getNovelChapterId, chapterId);
        List<NovelDetails> list = list(wrapper);
        if (CollectionUtils.isEmpty(list)){
            throw new CustomException("没有找到小说详细");
        }
        if (list.size()>1){
            throw new CustomException("小说详细获取异常，存在多条相同详细");
        }
        return list.get(0);
    }

    @Override
    public boolean delete(String novelId, String chapterId) {
        LambdaQueryWrapper<NovelDetails> wrapper = new LambdaQueryWrapper<NovelDetails>()
                .eq(NovelDetails::getNovelId,novelId)
                .eq(NovelDetails::getNovelChapterId, chapterId);
        return remove(wrapper);
    }

    @Override
    public boolean delete(String novelId) {
        LambdaQueryWrapper<NovelDetails> wrapper = new LambdaQueryWrapper<NovelDetails>()
                .eq(NovelDetails::getNovelId,novelId);
        return remove(wrapper);
    }
}